package com.resourcestore.action;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;
import com.resourcestore.utils.CommonUtil;

public class UploadFileAction extends BaseAction {

	private static final long serialVersionUID = 2854105381965508552L;

	private static final int BUFFER_SIZE = 20 * 1024; // 20K

	private File myFile; // 与页面 <input type="file"> 控件的 name 保持一致

	private String fileName; //
	private String contentType;

	public File getMyFile() {
		return myFile;
	}

	public void setMyFile(File myFile) {
		this.myFile = myFile;
	}

	public String getFileName() {
		return fileName;
	}

	// 是setMyFileFileName而不是setFileName 。 struts2的规则。File_upload 是上面private File
	// myFile

	public void setMyFileFileName(String fileName) {
		this.fileName = fileName;
	}

	public String getContentType() {
		return contentType;
	}

	// 同上
	public void setMyFileContentType(String contentType) {
		this.contentType = contentType;
	}

	// public String execute()
	// {
	// String newFileName = new Date().getTime() + getExtention(fileName);
	//
	// File imageFile = new File(ServletActionContext.getServletContext()
	// .getRealPath("/upload")
	// + "/" + newFileName);
	//
	// upload(myFile, imageFile);
	//
	// return SUCCESS;
	// }

	public String uploadFile() {
		String a = fileName;
		String newFileName = new Date().getTime() + this.getExtention(fileName);

		File imageFile = new File(ServletActionContext.getServletContext()
				.getRealPath("upload")
				+ "/" + newFileName);

		upload(myFile, imageFile);

		HttpServletRequest request = ServletActionContext.getRequest();
		String basePath = CommonUtil.urlPortDistinguish(request);
		
		HttpServletResponse response = ServletActionContext.getResponse();

		response.setCharacterEncoding("UTF-8");

		try {
			response.getWriter().print(basePath+"upload/"+newFileName);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return null;
	}

	private static void upload(File src, File dst) {
		try {
			InputStream in = null;
			OutputStream out = null;
			try {
				in = new BufferedInputStream(new FileInputStream(src),
						BUFFER_SIZE);
				out = new BufferedOutputStream(new FileOutputStream(dst),
						BUFFER_SIZE);
				byte[] buffer = new byte[BUFFER_SIZE];
				while (in.read(buffer) > 0) {
					out.write(buffer);
				}
			} finally {
				if (null != in) {
					in.close();
				}
				if (null != out) {
					out.close();
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private static String getExtention(String fileName) {
		int pos = fileName.lastIndexOf(".");

		return fileName.substring(pos);
	}

}
